<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta content="telephone=no" name="format-detection">
    <meta content="email=no" name="format-detection">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <link href="https://res.wx.qq.com/open/libs/weui/1.1.3/weui.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f8f8;
            padding: 0;
            margin: 0;
        }

        .header {
            border-bottom: 1px solid #ccc;
            background-color: #fff;
        }

        .footer {
            margin: 0;
            padding: 0;
        }

        .footer p {
            text-align: center;
            color: #999;
            font-weight: 200;
            margin-top: 20px;
            font-size: 12px;
        }

        a {
            font-size: 14px !important;
            font-weight: 300;
            padding: 5px 20px;
        }

        .logo {
            text-align: center;
            padding-top: 60px;
        }

        .logo img {
            width: 70px;
            opacity: 0.8;
            border-radius: 50%;
        }

        .amount {
            font-size: 48px;
            text-align: center;
            color: #333;
        }

        .mchname {
            color: #999;
            margin: 0 20px;
            border-top: 1px solid #eee;
            padding: 12px;
            font-size: 13px;
            font-weight: 200;
        }

        .popfeng {
            position: absolute;
            bottom: 40px;
            left: 0;
            right: 0;
            background-color: #f8f8f8;
            line-height: 2px;
        }

        .popfeng p {
            color: #bfbfbf;
            font-size: 12px;
            text-align: center;
            width: 100%;
        }
        #loading {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(255, 255, 255, 0.8);
            z-index: 9999;
            text-align: center;
        }

        #loading .weui-toast {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: none;
        }
    </style>
    <title>支付宝收银台1</title>
</head>
<body class="hide">
<div class="header">
    <div class="logo">
        <img src="https://img.hbhlpay.com/uploads/images/20240724/20240724161439f83fd1186.png">
    </div>
    <div class="amount">¥ <span id="amount"></span></div>
    <div class="mchname">收款说明: <span id="description"></span></div>
</div>

<div class="footer" style="padding:40px 20px;">
    <a class="weui-btn weui-btn_primary" href="javascript:" id="payBtn">立即支付</a>
    <p>支付完成后, 如需退款请及时联系卖家</p>
    <div class="popfeng">
        <p>由 汇联科技 提供支付技术服务</p>
    </div>
</div>

<div id="toast" style="opacity: 0; display: none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast">
        <i class="weui-icon-success-no-circle weui-icon_toast"></i>
        <p class="weui-toast__content">已完成</p>
    </div>
</div>
<div id="loading" style="display: none;">
    <div class="weui-mask"></div>
    <div class="weui-toast">
        <i class="weui-loading weui-icon_toast"></i>
        <p class="weui-toast__content">加载中...</p>
    </div>
</div>
<script src="https://cdn.bootcss.com/zepto/1.2.0/zepto.min.js"></script>
<script>
    // 调试时可以通过在页面定义一个元素，打印信息，使用alert方法不够优雅
    function log(obj) {
        $("#result").append(obj).append(" ").append("<br />");
    }
    // 显示加载提示
    function showLoading() {
        document.getElementById('loading').style.display = 'block';
    }

    // 隐藏加载提示
    function hideLoading() {
        document.getElementById('loading').style.display = 'none';
    }

    var payInfo = null;

    let domain = window.location.origin;
    let localUrl = window.location.href;
    // 获取URL中的 payOrderNo 参数
    const urlParams = new URLSearchParams(window.location.search);
    const payOrderNo = urlParams.get('payOrderNo');
    const authCode = urlParams.get('code');

    $(document).ready(function () {
        // 页面载入完成后即唤起收银台
        // 点击payButton按钮后唤起收银台
        $("#payBtn").click(function () {
            console.log("payBtn click")
            tradePay();
        });
        // 通过jsapi关闭当前窗口，仅供参考，更多jsapi请访问
        // /aod/54/104510
        $("#closeButton").click(function () {
            AlipayJSBridge.call('closeWebview');
        });

        if (!authCode || authCode == 'null') {
            alert("getAuthPath")
            getAuthPath();
        }
    });

    function getAuthPath() {
        fetch(`${domain}/openapi/oauth/alipay/auth?redirectUrl=${domain}${window.location.pathname}${window.location.search}`, {
            method: "GET",
            headers: new Headers({
                "Content-Type": "application/json;charset=UTF-8",
            }),
        })
            .then(response => response.json())
            .then(data => {
                // // 假设后端返回支付信息并将其赋值给 str
                if (data.code === 200) {
                    window.location.href = data.url;
                } else {
                    alert('获取授权链接失败：' + data.msg);
                }
            })
            .catch(error => {
                console.error('获取授权链接异常:', error);
            }).finally(() => {
            // 请求完成后，无论成功还是失败，都隐藏加载提示
            hideLoading();
        });
    }

    // 由于js的载入是异步的，所以可以通过该方法，当AlipayJSBridgeReady事件发生后，再执行callback方法
    function ready(callback) {
        if (window.AlipayJSBridge) {
            callback && callback();
        } else {
            document.addEventListener('AlipayJSBridgeReady', callback, false);
        }
    }

    function tradePay() {
        ready(function () {
            // 通过传入交易号唤起快捷调用方式(注意tradeNO大小写严格)
            AlipayJSBridge.call("tradePay", {
                tradeNO: payInfo.payInfo
            }, function (data) {
                if (data.resultCode != 9000) {
                    //支付失败
                    alert('支付失败');
                } else {
                    //支付成功
                    $(".alert-success").html("<strong>支付成功！</strong> 订单号：" + payInfo.payInfo);
                    $(".alert-success").show();
                }
            });
        });
    }

    if (payOrderNo && authCode) {
        // 显示加载提示
        showLoading();
        fetch(`${domain}/openapi/pay/pre/create`, {
            method: "POST",
            headers: new Headers({
                "Content-Type": "application/json;charset=UTF-8",
            }),
            body: JSON.stringify({payOrderNo})
        }).then(response => response.json())
            .then(data => {
                // 假设后端返回支付信息并将其赋值给 str
                if (data.code === 200) {
                    payInfo = data.data;
                    document.getElementById("amount").textContent = data.data.amount;
                    document.getElementById("description").textContent = data.data.subject;
                    $("#qr-pic").attr("src", "/static/payment/qrcode/images/successpay.png");
                    tradePay();
                } else {
                    console.error('请求支付信息失败:', data.msg);
                }
            })
            .catch(error => {
                console.error('请求支付信息失败:', error);
            }).finally(() => {
                // 请求完成后，无论成功还是失败，都隐藏加载提示
                hideLoading();
            }
        );
    }

</script>

</body>
</html>